/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.src.uniandes.ecos.tsp.creditscore.helper;

import com.src.uniandes.ecos.tsp.creditscore.entity.Cliente;
import com.src.uniandes.ecos.tsp.creditscore.entity.ClienteProducto;
import com.src.uniandes.ecos.tsp.creditscore.exception.CreditScoreException;
import com.src.uniandes.ecos.tsp.creditscore.jpa.ClienteJpaController;
import com.src.uniandes.ecos.tsp.creditscore.jpa.ClienteProductoJpaController;
import com.src.uniandes.ecos.tsp.creditscore.jpa.IndagacionJpaController;
import java.util.List;

/**
 *
 * @author AndhyMao
 */
public class CreditScoreHelper {

    protected static CreditScoreHelper creditScoreHelper;

    public static CreditScoreHelper getInstance() {
        if (creditScoreHelper == null) {
            creditScoreHelper = new CreditScoreHelper();
        }
        return creditScoreHelper;
    }

    public List<Cliente> findClienteEntities() throws CreditScoreException {
        List<Cliente> listClientes = null;
        try {
            ClienteJpaController clienteJpaController = new ClienteJpaController();
            listClientes = clienteJpaController.findClienteEntities();
        }
        catch (Exception e) {
            throw new CreditScoreException("CreditScoreHelper:findClienteEntities:" + e.toString());
        }
        return listClientes;
    }

    public List<ClienteProducto> findClienteProductoEntitiesByCliCodigoAndTipCodigo(Integer cliCodigo, Integer tipCodigo) throws CreditScoreException {
        List<ClienteProducto> listClientesProducto = null;
        try {
            ClienteProductoJpaController clienteProductoJpaController = new ClienteProductoJpaController();
            listClientesProducto = clienteProductoJpaController.findClienteProductoEntitiesByCliCodigoAndTipCodigo(cliCodigo, tipCodigo);
        }
        catch (Exception e) {
            throw new CreditScoreException("CreditScoreHelper:findClienteProductoEntitiesByCliCodigo:" + e.toString());
        }
        return listClientesProducto;
    }

    /**
     * 
     * @param clientCode
     * @param months
     * @return
     * @throws CreditScoreException
     */
    public int getLastMonthsCreditInquiries(final Integer clientCode,
            final Integer months) throws CreditScoreException {
        int creditInquiries = 0;
        try{
            IndagacionJpaController indagacionJpaController = new IndagacionJpaController();
            creditInquiries = indagacionJpaController.getLastMonthsCreditInquiries(clientCode, months);
        }catch (Exception e) {
            throw new CreditScoreException(
                    "CreditScoreHelper:getLastMonthsCreditInquiries:"
                    + e.toString());
        }
        return creditInquiries;
    }

    /**
     * 
     * @param clientCode
     * @return
     */
    public List<ClienteProducto> getCreditCardProducts(final Integer clientCode) throws CreditScoreException {
        List<ClienteProducto> products = null;
        
        try{
            ClienteProductoJpaController clienteProductoJpaController = new ClienteProductoJpaController();
            products = clienteProductoJpaController.getCreditCardProducts(clientCode);
        }catch (Exception e) {
            throw new CreditScoreException(
                    "CreditScoreHelper:getCreditCardProducts:"
                    + e.toString());
        }
        return products;
    }

    /**
     *
     * @param cliDoc
     * @param cliTDoc
     * @return
     * @throws CreditScoreException
     */
    public Integer getClienteByDocAndTypeDoc( final Integer cliTDoc, final Integer cliDoc )
            throws CreditScoreException{

         Integer codCliente = null;

         try{
            ClienteJpaController clienteJpaController = new ClienteJpaController();
            Cliente cliente = clienteJpaController.getClienteByDocAndDocType(cliTDoc, cliDoc);

            if(cliente != null){
                codCliente = cliente.getCliCodigo();
            }
        }catch (Exception e) {
            throw new CreditScoreException(
                    "CreditScoreHelper:getCreditCardProducts:"
                    + e.toString(), e);
        }
        return codCliente;
    }
}
